package presenter;

import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FilenameFilter;
import javax.swing.*;

public class Preferences extends JFrame implements ActionListener, WindowListener
{
    JCheckBox specificCoordinates;
    JPanel mainPanel;
    String[] fonts;
    JComboBox fontChooser;
    public static final long serialVersionUID=4;
    SimpleVariableFile text;
    JPanel fontPanel;
    JLabel fontLabel;
    JLabel bib;
    JComboBox bible;
    private JPanel jp;
    private JLabel x;
    private JLabel y;
    private JLabel width;
    private JLabel height;
    private JLabel fSize;
    private JTextField fontSize;
    private JTextField outputX;
    private JTextField outputY;
    private JTextField outputWidth;
    private JTextField outputHeight;
    private boolean applyPushed;
    private JButton apply;
    private JButton close;
    
    public Preferences(int outX, int outY, int outWidth, int outHeight, String fontName)
    {
        super("Preferences");
        
        mainPanel=new JPanel(new GridLayout(0,1));
        text=new SimpleVariableFile("TextInformation.txt");
        fonts=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        fontPanel=new JPanel(new GridLayout(4,2));
        fontLabel =new JLabel("Output Font");
        fontChooser=new JComboBox(fonts);
        fSize=new JLabel("Font Size");
        fontSize=new JTextField(""+TextInformation.fontSize);

        getBibles();
        fontPanel.add(fontLabel);
        fontPanel.add(fontChooser);
        fontPanel.add(fSize);
        fontPanel.add(fontSize);
        fontPanel.add(new JLabel(" "));
        fontPanel.add(new JLabel(" "));
        fontPanel.add(bib);
        fontPanel.add(bible);
        jp = new JPanel(new GridLayout(3, 4));
        x = new JLabel("Output X");
        y = new JLabel("Output Y");
        width = new JLabel("Output Width");
        height = new JLabel("Output Height");
        outputX = new JTextField();
        outputY = new JTextField();
        outputWidth = new JTextField();
        outputHeight = new JTextField();
        apply = new JButton("Apply");
        close = new JButton("Close");
        if(text.exists("Coordinates")&&text.getVariable("Coordinates").equals("true")) {
            specificCoordinates=new JCheckBox("Use Specific Output Coordinates", true);
        }
        else {
            specificCoordinates=new JCheckBox("Use Specific Output Coordinates", false);
        }
        jp.add(x);
        jp.add(outputX);
        jp.add(y);
        jp.add(outputY);
        jp.add(width);
        jp.add(outputWidth);
        jp.add(height);
        jp.add(outputHeight);
        jp.add(new JLabel(" "));
        jp.add(new JLabel());
        jp.add(apply);
        jp.add(close);
        outputX.setText((new StringBuilder()).append("").append(outX).toString());
        outputY.setText((new StringBuilder()).append("").append(outY).toString());
        outputWidth.setText((new StringBuilder()).append("").append(outWidth).toString());
        outputHeight.setText((new StringBuilder()).append("").append(outHeight).toString());
        mainPanel.add(fontPanel);
        mainPanel.add(specificCoordinates);
        mainPanel.add(jp);
        for(int i=0; i<fonts.length; i++) {
            if(fonts[i].equals(fontName)) {
                fontChooser.setSelectedIndex(i);
                break;
            }
        }    
        setContentPane(mainPanel);
        setLocation(200, 200);
        setDefaultCloseOperation(2);
        pack();
        setVisible(true);
        addWindowListener(this);
    }

    public int getOutputX()
    {
        return Integer.parseInt(outputX.getText());
    }

    public int getOutputY()
    {
        return Integer.parseInt(outputY.getText());
    }

    public int getOutputWidth()
    {
        return Integer.parseInt(outputWidth.getText());
    }

    public int getOutputHeight()
    {
        return Integer.parseInt(outputHeight.getText());
    }

    public boolean isApplyPushed()
    {
        return applyPushed;
    }

    public JButton getApplied()
    {
        return apply;
    }

    public JButton getClose()
    {
        return close;
    }
    
    public boolean getSpecific() {
        return specificCoordinates.isSelected();
    }

    public String getFontName() {
        return (String)fontChooser.getSelectedItem();
    }
    
    public int getFontSize() {
        return 0;
    }

    public void windowOpened(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowClosing(WindowEvent e) {
        text.update("Font", getFontName());
        System.out.println("updating");
        if(!text.exists("FontSize")) {
            text.add("FontSize", fontSize.getText());
        } else
            text.update("FontSize", fontSize.getText());
        //}
        text.write();
    }

    public void windowClosed(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowIconified(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeiconified(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowActivated(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeactivated(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==apply) {
            TextInformation.fontSize=Integer.parseInt(fontSize.getText());
            TextInformation.staticFontSize=true;
            System.out.println("Font updated to "+TextInformation.fontSize);
            BibleInformation.defaultBible=bible.getSelectedItem().toString();
        }
    }
    
    private void getBibles() {
        File f=new File("Bibles");
        String[] bibs=f.list(new BibleFilter());
        for(int i=0; i<bibs.length; i++) {
            bibs[i]=bibs[i].substring(0,bibs[i].lastIndexOf(".xmm"));
        }
        bib=new JLabel("Default Bible");
        bible=new JComboBox(bibs);
    }
}
